package com.hnkywl.sts.service.organ;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.organ.NbbmDao;
import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.OrderBy;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.organ
 * 类描述:    内部部门service类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class NbbmService {
    private NbbmDao nbbmDao;
    private GsjgDao gsjgDao;
    private BmService bmService;

    public String findAllNbbm(Long id, String mc, Long parentId, String parentMc, int pageNo, int pageSize) {
        Page<Nbbm> nbbmPage = nbbmDao.findNbbmList(id, mc, parentId, parentMc, pageNo, pageSize);
        String nbbmJson = BaseUtils.fetchCollectionToJson(nbbmPage.getResult() == null ? new ArrayList<Nbbm>() : nbbmPage.getResult(),
                "id", "qc", "jc", "sjjg.jc", "bz", "sfzx", "bmjl.xm", "rcgl.xm", "bmzz", "sfjyzz", "sfzjzy", "khbm.jc");
        return "{\"total\":" + nbbmPage.getTotalCount() + ",\"rows\":[" + nbbmJson + "]}";
    }

    public String findAllNbbmMc(Long id, String mc, Long parentId, String parentMc, int pageNo, int pageSize) {
        Page<Nbbm> nbbmPage = nbbmDao.findNbbmList(id, mc, parentId, parentMc, pageNo, pageSize);
        String nbbmJson = BaseUtils.fetchCollectionToJson(nbbmPage.getResult() == null ? new ArrayList<Nbbm>() : nbbmPage.getResult(),
                "id", "qc", "jc");
        return "[" + nbbmJson + "]";
    }

    /**
     * 查询内部部门明细
     *
     * @param id 部门Id
     * @return
     */
    public Nbbm findNbbmDetail(Long id) {
        return nbbmDao.get(id);
    }

    /**
     * 公司机构树形展示
     *
     * @param id 树节点Id
     */
    public String findAllNbbm(Long id) {
        List<Nbbm> nbbmList = new ArrayList<Nbbm>();
        if (id != null)
            nbbmList.addAll(nbbmDao.findByProperty("sjjg.id", id, new OrderBy[]{new OrderBy("pxh", OrderBy.OrderType.ASC)}));
        else
            nbbmList.addAll(nbbmDao.findAllRootNbbm());
        Map<String, String> treeParam = new HashMap<String, String>();
        treeParam.put("text", "jc");
        treeParam.put("id", "id");
        Map<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("iconCls", "icon-tree-nbbm");
        return BaseUtils.getTreeJson(nbbmList, treeParam);
    }

    /**
     * 供应站查询
     *
     * @return
     */
    public String getGyzJson(Long gsId) {
        if (gsId == null) {
            return "[]";
        }
        List<Nbbm> nbbmList = new ArrayList<Nbbm>();
        nbbmList.addAll(nbbmDao.findGyz(gsId));
        if (nbbmList == null)
            return "[]";
        return "[" + BaseUtils.fetchCollectionToJson(nbbmList, "id", "jc") + "]";
    }

    /**
     * 保存内部部门信息
     *
     * @param nbbm 内部部门实体
     * @return
     */
    @Transactional
    public void saveNbbm(Nbbm nbbm) {
        String  bm = this.getNbbmbm();
        nbbm.setBm(bm);
        nullId(nbbm);
//        if (nbbmDao.checkUnique(nbbm.getId(),nbbm.getQc(),"qc")) {
//            throw new RuntimeException("此全称已存在");
//        }
//        if (nbbmDao.checkUnique(nbbm.getId(),nbbm.getJc(),"jc")) {
//            throw new RuntimeException("简称已存在");
//        }
//        if (nbbmDao.checkUnique(nbbm.getId(),nbbm.getBm(),"bm")) {
//            throw new RuntimeException("编码重复");
//        }
        nbbm.setDateCreated(new Date());
        nbbm.setSfzjzy(nbbm.getSfjyzz());
        nbbm.setSfzx(!nbbm.getSfzx());
        nbbm.setLastUpdated(nbbm.getDateCreated());
        nbbmDao.save(nbbm);
    }

    //内部部门编码
    public String getNbbmbm() {
        return bmService.getJgbm();
    }

    /**
     * 修改内部部门信息
     *
     * @param nbbm 内部部门实体
     * @return
     */
    @Transactional
    public void updateNbbm(Nbbm nbbm) {
        nullId(nbbm);
//        if (nbbmDao.checkUnique(nbbm.getId(),nbbm.getQc(),"qc")) {
//            throw new RuntimeException("此全称已存在");
//        }
//        if (nbbmDao.checkUnique(nbbm.getId(),nbbm.getJc(),"jc")) {
//            throw new RuntimeException("简称已存在");
//        }
//        if (nbbmDao.checkUnique(nbbm.getId(),nbbm.getBm(),"bm")) {
//            throw new RuntimeException("编码重复");
//        }
        nbbm.setSfzjzy(nbbm.getSfjyzz());
        nbbm.setSfzx(!nbbm.getSfzx());
        Nbbm n = nbbmDao.get(nbbm.getId());
        n = (Nbbm) ReflectionUtils.getInstance(n, nbbm);
        n.setLastUpdated(new Date());
        nbbmDao.save(n);
    }

    /**
     * 删除内部部门信息
     *
     * @param id 内部部门Id
     * @return
     */
    @Transactional
    public void deleteNbbm(Long id) {
        Nbbm nbbm = nbbmDao.get(id);
        nbbm.setDeleted(1);
        nbbm.setLastUpdated(new Date());
        nbbmDao.save(nbbm);
    }

    private void nullId(Nbbm nbbm) {
        if (nbbm != null && nbbm.getYjg() != null && nbbm.getYjg().getId() == null)
            nbbm.setYjg(null);
        if (nbbm != null && nbbm.getSjjg() != null && nbbm.getSjjg().getId() == null)
            nbbm.setSjjg(null);
        if (nbbm != null && nbbm.getBmjl() != null && nbbm.getBmjl().getId() == null)
            nbbm.setBmjl(null);
        if (nbbm != null && nbbm.getRcgl() != null && nbbm.getRcgl().getId() == null)
            nbbm.setRcgl(null);
        if (nbbm != null && nbbm.getKhbm() != null && nbbm.getKhbm().getId() == null)
            nbbm.setKhbm(null);
        if (nbbm != null && nbbm.getKhgs() != null && nbbm.getKhgs().getId() == null)
            nbbm.setKhgs(null);
        if (nbbm != null && nbbm.getCbfy() != null && nbbm.getCbfy().getId() == null)
            nbbm.setCbfy(null);
        if (nbbm != null && nbbm.getYwkh() != null && nbbm.getYwkh().getId() == null)
            nbbm.setYwkh(null);
        if (!CollectionUtils.isEmpty(nbbm.getGlckList()) && nbbm.getGlckList().get(0).getId() == null)
            nbbm.setGlckList(null);
    }

    /**
     * 获取经营部门
     *
     * @param gsjgId
     * @return
     */
    public String findJybm(Long gsjgId) {
        List<Nbbm> list = nbbmDao.findJybm(gsjgId);
        String json = BaseUtils.fetchCollectionToJson(list, "id", "qc");
        return "[" + json + "]";
    }

    /**
     * 保供业务部门
     * @return
     */
    public String findBgywbm() {
        List<Nbbm> list = nbbmDao.findBgywbm();
        String json = BaseUtils.fetchCollectionToJson(list, "id", "qc");
        return "[" + json + "]";
    }

    /**
     * 获取部门科目代码
     *
     * @param gsId
     * @return
     */
    public String findKmdm(Long gsId) {
        Gson gson = new Gson();
        return gson.toJson(nbbmDao.findKmdm(gsId));
    }

    @Autowired
    public void setNbbmDao(NbbmDao nbbmDao) {
        this.nbbmDao = nbbmDao;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
}
